const fs = require("fs")
const express = require("express")
const student = require("./student")
//创建一个路由容器
const router = express.Router()

//将路由挂载到容器里
//首页
router.get('/student', function (req, res) {
    student.readStudentList(function (json) {
        res.render("index.html", {
            list: [
                "苹果", "香蕉", "梨子", "橙子"
            ],
            studentList: json.studentList
        })
    })
});


//跳转到添加路径
router.get('/student/add', function (req, res) {
    res.render("add.html")
});

//添加
router.post('/student/add', function (req, res) {
    student.add(req.body, function () {
        res.redirect("/student")
    })
});


//跳转到修改路径
router.get('/student/edit', function (req, res) {
    student.getById(req.query.id, function (editStudent) {
        res.render("add.html", {
            student: editStudent
        })
    })
});


//修改
router.post('/student/edit', function (req, res) {

    student.update(req.body, function () {
        res.redirect("/student")
    })

})
;


//删除
router.get('/student/delete', function (req, res) {
    student.deleteById(req.query.id,function () {
        res.redirect("/student")
    })
});

module.exports = router